package com.yuanian.timingwheel.respository.po;

import com.yuanian.timingwheel.context.SpringContext;
import com.yuanian.timingwheel.dao.LivingServerDao;
import com.yuanian.timingwheel.statistics.LivingServer;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

//@TableName(value = "TP_TW_LIVINGSERVER")
public class LivingServerEntity  {

    //@TableId(type = IdType.AUTO)
    private Long id;

    private String serverIp;

    //最后一次运行时间,供查看是否在运行
    private Date lastRespondTime;

    //卡顿总秒数
    private long delayCnt = 0;

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public Date getLastRespondTime() {
        return lastRespondTime;
    }

    public void setLastRespondTime(Date lastRespondTime) {
        this.lastRespondTime = lastRespondTime;
    }

    public long getDelayCnt() {
        return delayCnt;
    }

    public void setDelayCnt(long delayCnt) {
        this.delayCnt = delayCnt;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public static LivingServerEntity getLivingServer() {
        /*QLivingServerEntity qLivingServerEntity = new QLivingServerEntity();
        return qLivingServerEntity.findOne();*/
        //return SpringContext.getBean(Database.class).find(LivingServerEntity.class).findOne();
        return SpringContext.getBean(LivingServerDao.class).getLivingServer();

    }

    public static boolean existLivingServer() {
        LivingServerEntity entity = getLivingServer();
        if (entity == null) {
            return false;
        }
        //最后响应时间是否小于30秒
        return (new Date().getTime() - entity.getLastRespondTime().getTime()) / 1000 < 30;
    }

    public static LivingServer createLivingServer() {

        InetAddress addr = null;
        String ip = "";
        try {
            addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        SpringContext.getBean(LivingServerDao.class).deleteLivingServer();
       /* LivingServerEntity existEntity = SpringContext.getBean(Database.class).find(LivingServerEntity.class).findOne();
        if (existEntity != null) {
            existEntity.delete();
        }*/
        //QLivingServerEntity qLivingServerEntity = new QLivingServerEntity();
        //删除老的
        //qLivingServerEntity.delete();
        //插入新的
        LivingServerEntity entity = new LivingServerEntity();
        entity.setServerIp(ip);
        entity.setDelayCnt(0);
        entity.setLastRespondTime(new Date());
        SpringContext.getBean(LivingServerDao.class).insertLivingServer(entity);
        LivingServer livingServer = new LivingServer(entity.getId(), ip);
        return livingServer;

    }

    public static void updateLivingServer(Long id, Date lastRespondTime, long delayCnt) {
       /* QLivingServerEntity qLivingServerEntity = new QLivingServerEntity();
        LivingServerEntity entity = qLivingServerEntity.id.eq(id).findOne();
        entity.setDelayCnt(delayCnt);
        entity.setLastRespondTime(lastRespondTime);
        entity.save();*/
        /*LivingServerEntity entity = SpringContext.getBean(Database.class).find(LivingServerEntity.class).findOne();
        entity.setDelayCnt(delayCnt);
        entity.setLastRespondTime(lastRespondTime);
        entity.save();*/

    }

}
